/*
 * Capstone.h
 *
 * Created: 1/18/2013 8:49:04 PM
 *  Author: Alexander
 */ 


#ifndef CAPSTONE_H_
#define CAPSTONE_H_

// AVR


#include <avr/io.h>

#include <stdio.h>
#include <stdint.h>

// AVRx
#include "avrx-io.h"
#include "avrx-signal.h"
#include "avrx.h"
#include "serialio.h"       // From AvrX...
#include "avrx_hardware_custom.h"
#include "AvrXYieldHack.h"

// SWP
#include "Gyro.h"
#include "Vector_Class.h"
#include "Motor_Driver.h"
#include "U0_UART.h"
#include "Pressure_Sensor.h"
//#include "Motor_Driver.h"


/************************************************************************/
/* DEFINITIONS															*/
/************************************************************************/

/*********************PORTB*********************/
#define P_Gyro				PB0	//Gyro SS (Active Low)
#define P_SCK				PB1	//SPI SCLK
#define P_MOSI				PB2	//SPI MOSI
#define P_MISO				PB3	//SPI MISO
#define P_Something_PWM		PB4	//0C0A
#define P_Something2_PWM	PB5	//0C1A
#define P_Something3_PWM	PB6	//0C1B
/*DO NOT USE, AvrX uses Timer 2
#define P_Something4_PWM	PB7	//0C2A
*/

/*********************PORTD*********************/

#define P_Pump				PD0	// Water Pump
#define P_Solenoid			PD1	// Solenoid
#define P_Invert			PD2 // Motor_Invert
#define P_Motor_Status_Flag	PD3	// Status Flag for Motor Driver
#define P_Pressure			PD4	//Pressure Sensor SS (Active Low)


extern FILE uart_stream;

AxesVector_ptr Angle_ptr;
AxesVector_ptr Current_Angle_ptr;
AxesVector_ptr Previous_Vector_ptr;

void Gyro_Init(void);
int main(void);
void Calculate_Angle(AxesVector_t* zero_rate_vector, AxesVector_t* vector, AxesVector_t* prev_vector);
void Vector_Init(AxesVector_t* vector);
void Timer_Init();
void Motor_Adjust();

Mutex turnServos;

 AVRX_GCC_TASKDEF(Gyro, 200, 1);
 AVRX_GCC_TASKDEF(GyroAverage, 100, 2);
 AVRX_GCC_TASKDEF(Pump, 0, 2);
 AVRX_GCC_TASKDEF(Pump_Forward, 0, 2);
 AVRX_GCC_TASKDEF(Pump_Backward, 0, 2);
 AVRX_GCC_TASKDEF(Pressure_Sensor, 0, 2);
// AVRX_GCC_TASK(Pump, 0, 2);
// AVRX_GCC_TASK(Servos, 0,3);
// AVRX_GCC_TASK(Pressure_Sensor, 0, 3);


/*********************AVRX Control Blocks*********************/

MessageQueue PumpQueue;
Mutex	Pump_Forward_Done, Pump_Backward_Done, Pump_Backward_Mutex, Pump_Forward_Mutex;
TimerControlBlock Gyro_TCB, Gyro_Average_TCB, PumpTime_TCB;

/*********************GLOBAL DEFS*********************/

	#define	GyroDelay		5	//ms
	#define Divetime		17	//s
	#define MaxPumpTime		20	//s
	#define MAX_PRESSURE	8500	// Max Pressure That can be blown into the tube
	#define MIN_PRESSURE	8000	//unsigned pressure Data (about Atmospheric Pressure)
	
#endif /* CAPSTONE_H_ */